package com.study.order.service.impl;

import com.study.order.bean.Order;
import com.study.order.fegin.ProductFeignClient;
import com.study.order.service.IOrderService;
import com.study.product.bean.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: WPX
 * @Create: 2025/07/31
 **/
@Slf4j
@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private ProductFeignClient productFeignClient;

    @Override
    public Order createOrder(Long userId, Long productId) {
//        Product product = getProductFromRemoteWithLoadBalanceAnnotate(productId);
        // 使用Feign
        Product product = productFeignClient.getById(productId);

        Order order = new Order();
        order.setId(1L);
        // 订单总金额
        order.setTotalAmount(product.getPrice().multiply(new BigDecimal(product.getNum())));
        order.setUserId(userId);
        order.setNickname("zhangsan");
        order.setAddress("翻斗花园");
        // 远程查询商品列表
        order.setProducts(Arrays.asList(product));
        return order;
    }

    /**
     * @Description: 第三种方式 注解式负载均衡
     * @AuthorCreate: WPX 2025/8/1
     **/
    private Product getProductFromRemoteWithLoadBalanceAnnotate(Long productId) {
        // 1、获取到商品服务所在的所有机器IP+Port
        String url = "http://service-product/product/" + productId;
        log.info("远程请求：{}", url);
        // 2、给远程发送请求
        return restTemplate.getForObject(url, Product.class);
    }

    /**
     * @Description: 第二种方式 负载均衡
     * @AuthorCreate: WPX 2025/8/1
     **/
    private Product getProductFromRemoteWithLoadBalance(Long productId) {
        // 1、获取到商品服务所在的所有机器IP+Port
        ServiceInstance choose = loadBalancerClient.choose("service-product");
        String url = "http://" + choose.getHost() + ":" + choose.getPort() + "/product/" + productId;
        log.info("远程请求：{}", url);
        // 2、给远程发送请求
        return restTemplate.getForObject(url, Product.class);
    }

    /**
     * @Description: 第一种方式
     * @AuthorCreate: WPX 2025/8/1
     **/
    private Product getProductFromRemote(Long productId) {
        // 1、获取到商品服务所在的所有机器IP+Port
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance instance = instances.get(0);
        String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/product/" + productId;
        log.info("远程请求：{}", url);
        // 2、给远程发送请求
        return restTemplate.getForObject(url, Product.class);
    }
}
